<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Cookie;


class Index extends Controller
{
    public function getchart()
    {
        return "eyJoYW5ncWluZyI6Ilx1NTU0Nlx1NTRjMVx1ODg0Y1x1NjBjNSIsImppYW95aWppbHUiOiJcdTRlYTRcdTY2MTNcdThiYjBcdTVmNTUiLCJzaGFuZ3Bpbm1pbmdjaGVuZyI6Ilx1NTU0Nlx1NTRjMVx1NTQwZFx1NzlmMCIsInhpYW5qaWEiOiJcdTczYjBcdTRlZjciLCJ6dWlkaSI6Ilx1NjcwMFx1NGY0ZSIsInp1aWdhbyI6Ilx1NjcwMFx1OWFkOCJ9";
    }


    private function lang()
    {
        if (!empty($_GET['lang'])) {
            switch ($_GET['lang']) {
                case 'zh-cn':
                    {
                        cookie('think_var', 'zh-cn');
                        break;
                    }
                case 'en-us':
                    {
                        cookie('think_var', 'en-us');
                        break;
                    }
                case 'zh-tw':
                    {
                        cookie('think_var', 'zh-tw');
                        break;
                    }
            }
        }

        return cookie('think_var');
    }


    /**
     * 首页 行情列表
     * @author lukui  2017-02-18
     * @return [type] [description]
     */
    public function index()
    {
        $lang = $this->lang();
        $purl = Db::name('config')->find(27);
        //获取产品信息
        $pro = Db::name('productinfo')
            ->alias('pi')
            ->field('pi.pid,pi.suoxie,pi.color,pi.ptitle,pd.Price,pd.UpdateTime,pd.Low,pd.High')
            ->join('__PRODUCTDATA__ pd', 'pd.pid=pi.pid')
            ->where('pi.isdelete', 0)->order('pi.pid desc')->select();
//        dump(Db::getlastsql());
        if (!empty($pro)) {
            foreach ($pro as $key => $v) {
                if ($v['Price'] < cookie('pid' . $v['pid'])) {  //跌了
                    $pro[$key]['isup'] = 0;
                } elseif ($v['Price'] > cookie('pid' . $v['pid'])) {  //涨了
                    $pro[$key]['isup'] = 1;
                } else {  //没跌没涨
                    $pro[$key]['isup'] = 2;
                }
//                dump('pid' . $v['pid'].":".cookie('pid' . $v['pid']));
//                dump("price:".$v['Price']);
                if(cookie('pid' . $v['pid'])){
                  $pid = cookie('pid' . $v['pid']);
//                  dump($pid);
                    $pro[$key]['zf'] =round(($v['Price']-$pid) /$pid,3);
                }else{
                    $pro[$key]['zf'] = 0;
                }
            }
        }
        $notice = Db::name('notice')->select();

        $this->assign('notice', $notice);
        $this->assign('pro', $pro);
        $this->assign('lang', $lang);
        $this->assign('purl', $purl);
        return $this->fetch();
    }

    public function ajaxindexpro()
    {
        //获取产品信息
        $pro = Db::name('productinfo')
            ->alias('pi')
            ->field('pi.pid,pi.ptitle,pi.point_low,pi.point_top,pd.Price,pd.UpdateTime,pd.Low,pd.High')
            ->join('__PRODUCTDATA__ pd', 'pd.pid=pi.pid')
            ->where('pi.isdelete', 0)->order('pi.pid desc')->select();
        $newpro = array();
        foreach ($pro as $k => $v) {
            $newpro[$v['pid']] = $pro[$k];
            $newpro[$v['pid']]['UpdateTime'] = date('H:i:s', $v['UpdateTime']);
			$pid =  cookie('pid' . $v['pid']);

			$Price = $this->fengkong($v['Price'], $v);
            $newpro[$v['pid']]['Price'] = $Price;

            $newpro[$v['pid']]['zf'] = !empty($pid)?round(($Price-$pid) /$pid,5):0;
//            dump($newpro[$v['pid']]['zf']);
            $newpro[$v['pid']]['zf'] = $newpro[$v['pid']]['zf'] * 100;
            /*if($v['ptitle'] == "BTC" || $v['ptitle'] == "ETH"){
                $newpro[$v['pid']]['zf'] = $newpro[$v['pid']]['zf'] * 1000;
            }*/
            // if(!isset($_COOKIE['pid'.$v['pid']])){
            //     cookie('pid'.$v['pid'],$Price);
            //     continue;
            // }
            if ($Price < cookie('pid' . $v['pid'])) {  //跌了
                $newpro[$v['pid']]['isup'] = 0;
            } elseif ($Price > cookie('pid' . $v['pid'])) {  //涨了
                $newpro[$v['pid']]['isup'] = 1;
            } else {  //没跌没涨
                $newpro[$v['pid']]['isup'] = 2;
            }
            cookie('pid' . $v['pid'], $Price);
        }
//        dump($newpro);
        return $newpro;
    }

    /**
     * 数据风控
     * @param  [type] $price [description]
     * @param  [type] $pro   [description]
     * @return [type]        [description]
     * @author lukui  2017-06-27
     */
    public function fengkong($price, $pro)
    {
//        dump("ptitle：".$pro['ptitle']);
        $point_low = $pro['point_low'];
        $point_top = $pro['point_top'];

        $FloatLength = getFloatLength($point_top);
        $jishu_rand = pow(10, $FloatLength);
//        dump("jishu_rand：".$jishu_rand);
        $point_low = $point_low * $jishu_rand;
        $point_top = $point_top * $jishu_rand;
        $rand = rand($point_low, $point_top) / $jishu_rand;
//        dump("rand：".$rand);
        $_new_rand = rand(0, 10);
        if ($_new_rand % 2 == 0) {
            $price = $price + $rand;
        } else {
            $price = $price - $rand;
        }
//        dump($price);
        return $price;
    }

    protected function fetch($template = '', $vars = [], $replace = [], $config = [])
    {
        $replace['__HOME__'] = str_replace('/index.php', '', \think\Request::instance()->root()) . '/static/index';
        return $this->view->fetch($template, $vars, $replace, $config);
    }
}
